var pageCount = 0;
var pageTag;

mui('#pullrefresh1').scroll({
    deceleration: 0.0005 //flick 减速系数，系数越大，滚动速度越慢，滚动距离越小，默认值0.0006
});
// 重置
function reset(){
    pageCount = 0;
    var table = document.body.querySelector('#list-view');
    table.innerHTML='';
    mui('#pullrefresh').pullRefresh().refresh(true);
    pullupRefresh()
}

mui.ready(function () {
    // 左侧名字点击事件
    mui("#pullrefresh1").on('tap','.mui-table-view-cell',function () {
        // console.log("xxx",this)
        // 重置class
        var cellls = mui("#pullrefresh1 .mui-table-view-cell");
        cellls.each((index,item)=>{
            // console.log(index,mui(item)[0])
            mui(item)[0].className = 'mui-table-view-cell'
        })
        // active setting
        mui(this)[0].className='mui-table-view-cell mui-activeing';

        // 重置
        reset()
    })

    // 切换
    mui("#segmentedControl").on("tap","a",function () {
        var tagIndex = this.getAttribute('tag'); // day || month
        // console.log('tabs',tabs)
        pageTag = tagIndex;
        // 重置
        reset()
    })


    // 上一步
    mui('.mui-bar').on('tap','#prevBtn',function () {

        // 返回按钮
        mui("#backBtn")[0].style.display = 'block';
        mui("#prevBtn")[0].style.display = 'none';

        mui("#segmentedControlbox")[0].style.display = 'block';
        mui("#pullrefresh1")[0].style.display = 'block';
        mui("#pullrefresh")[0].style.display = 'block';
        mui("#detail")[0].style.display = 'none';
    })

})


mui.init({
    pullRefresh: {
        container: '#pullrefresh',
        down: {
            style:'circle',
            callback: pulldownRefresh
        },
        up: {
            auto:true,
            contentrefresh: '正在加载...',
            callback: pullupRefresh
        }
    }
});


function pullupRefresh() {
    setTimeout(function() {
        mui('#pullrefresh').pullRefresh().endPullupToRefresh((++pageCount > 2)); //参数为true代表没有更多数据了。
        var table = document.body.querySelector('#list-view');
        var cells = document.body.querySelectorAll('#list-view .mui-table-view-cell');
        var newCount = cells.length>0?5:20;//首次加载20条，满屏
        for (var i = cells.length, len = i + newCount; i < len; i++) {
            var li = document.createElement('li');
            // li.className = 'mui-table-view-cell mui-media';
            // li.innerHTML = '<a class="mui-navigate-right">Item ' + (i + 1) + '</a>';
            li.innerHTML =`<li class="month">
                <div class="mui-input-row mui-tit">
                    <span class="line"></span>
                    <label>2019-09-09</label>
                </div>
            </li>
            <li class="mui-navigate-right" tag="serviceCount">
                <div class="mui-input-row">
                    <label>服务商总数</label>
                    <span class="mui-pull-right">1</span>
                </div>
            </li>
            <li class="mui-navigate-right" tag="merchantCount">
                <div class="mui-input-row">
                    <label>商户总数</label>
                    <span class="mui-pull-right">1</span>
                </div>
            </li>
            <li class="mui-navigate-right" tag="today">
                <div class="mui-input-row">
                    <label>当日总交易额</label>
                    <span class="mui-pull-right">1</span>
                </div>
            </li>
            <li class="mui-navigate-right" tag="thismonth">
                <div class="mui-input-row">
                    <label>当月总交易额</label>
                    <span class="mui-pull-right">1</span>
                </div>
            </li>`;
            table.appendChild(li);
        }
        bindClick(li);
    }, 500);
}

function addData() {
    var table = document.body.querySelector('#list-view');
    var cells = document.body.querySelectorAll('#list-view .mui-table-view-cell');
    for(var i = cells.length, len = i + 5; i < len; i++) {
        var li = document.createElement('li');
        li.className = 'mui-table-view-cell';
        li.innerHTML = '<a class="mui-navigate-right">Item ' + (i + 1) + '</a>';
        //下拉刷新，新纪录插到最前面；
        table.insertBefore(li, table.firstChild);
        bindClick(li);
    }
}
/**
 * 下拉刷新具体业务实现
 */
function pulldownRefresh() {
    setTimeout(function() {
        addData();
        mui('#pullrefresh').pullRefresh().endPulldownToRefresh();
        mui.toast("为你推荐了5篇文章");
    }, 1000);
}

function bindClick(ele) {

    mui("#list-view").off("tap","li");
    mui("#list-view").on('tap',"li",function () {
        // console.log(this.getAttribute('tag'))
        var tag = this.getAttribute('tag'); // 4个判断标识
        console.log('tag',tag);
        // detail
        if(tag){
            // 返回按钮
            mui("#backBtn")[0].style.display = 'none';
            mui("#prevBtn")[0].style.display = 'block';

            mui("#segmentedControlbox")[0].style.display = 'none';
            mui("#pullrefresh1")[0].style.display = 'none';
            mui("#pullrefresh")[0].style.display = 'none';
            mui("#detail")[0].style.display = 'block';
        }
    })
}